package group6renderer.renderernode;

import group6renderer.Display;
import group6renderer.GUI;
import group6renderer.Visitor;

/**
*
* @author Yiannis
*/

public class TitleNode extends BranchNode {
	
    private String htmlTitle;
	
    
    @Override
    public void addChild(TreeNode child) throws Exception
    {
    	if (child instanceof TextNode)
    		super.addChild(child);
    	else
    		throw new Exception("Only text is allowed in title tag");
    }
    
    public TitleNode(String name)
    {
    	this.name=name;
    	htmlTitle=new String("");
    }
    
    public void drawNode(Display disp)
    {
    	if (goingDown)
    	{getTitle();
    	GUI.guiInstance().setTitle(htmlTitle);
    	goingDown=false;
    	}
    }
	
	public  boolean isLeaf()
    {
    	return false;
    }
    public boolean hasClosing()
    {
    	return true;
    }

    public TreeNode copy()
    {
    	return new TitleNode(name);
    }
    @Override
	public void accept(Visitor theVisitor) {
		theVisitor.visit(this);
	}
    
    
    public void setTitle(String htmlTitle)
    {
    	this.htmlTitle=htmlTitle;
    }
   
    public String getTitle()
    {

    	int i=0;
    		while(i<this.getChildrenNum())
    		{
    			TextNode t=(TextNode)getChild(i);
    		
    			htmlTitle=htmlTitle+" "+t.getText();
    			removeChild(i);
    		}

    	
    	return htmlTitle;
    }

}